<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/5/27
 * Time: 11:46
 */
namespace Admin\Controller;
use Think\Controller;

class MasterOrderController extends CommonController
{
    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1='财务管理';
        $this->breadcrumb2='师傅维修订单收入管理';
        $this->url=U('MasterOrder/index');
        $this->sqlname='masterorder'.session('user_auth')['uid'];
    }

    public function index(){
       $date1=I('post.time1','','string');
       $date2=I('post.time2','','string');
       $order_id=I('post.order_id');
       $master_id=I('post.master_id','','string');
       $master_name=I('post.master_name','','string');
        if($date1 && $date2){
            $where['ro.pay_time']=array('gt',strtotime($date1));
            $where['ro.pay_time']=array('lt',strtotime($date2));
        }
        if($order_id){
            $where['ro.repair_order_num_alias']=$order_id;
        }
        if($master_id){
            $where['ro.worker_tel']=$master_id;
        }

        if($master_name){
            $where['ifo.id_name']=array('like',"%$master_name%");
        }

        $where['ro.pay_time']=array('neq',0);

     //   $total=M('repair_order')->where("pay_time>0")->field('sum(total) total')->find();
        $order_count=M('repair_order ro')->join('left join zsf_user_real_info ifo on ro.user_id=ifo.user_id')
            ->join('left join zsf_volume_list l on ro.volume_list_id=l.volume_list_id')
            ->join('left join zsf_rewards_money m on ro.repair_order_num_alias=m.order_id')
            ->join('left join zsf_repair_order_protocol p on ro.repair_order_num_alias=p.order_id')
            ->where($where)
            ->count();
        $Page       = new \Think\Page($order_count,25);

        //记录sql语句 导出excel时候用
        $sql=M('repair_order ro')->join('left join zsf_user_real_info ifo on ro.user_id=ifo.user_id')
            ->join('left join zsf_volume_list l on ro.volume_list_id=l.volume_list_id')
            ->join('left join zsf_rewards_money m on ro.repair_order_num_alias=m.order_id')
            ->join('left join zsf_repair_order_protocol p on ro.repair_order_num_alias=p.order_id')
            ->where($where)
            ->field('ro.pay_time,ro.fault_money,p.maintain,p.bond,ro.repair_order_num_alias,ro.repair_order_id,ifo.id_name,ro.worker_tel,ro.total-ro.diagnosis_cost total,l.total vtotal,ro.diagnosis_cost,ro.tip,m.money')
            ->select(false);

        $total=M('repair_order ro')->join('left join zsf_user_real_info ifo on ro.user_id=ifo.user_id')
            ->join('left join zsf_volume_list l on ro.volume_list_id=l.volume_list_id')
            ->join('left join zsf_rewards_money m on ro.repair_order_num_alias=m.order_id')
            ->join('left join zsf_repair_order_protocol p on ro.repair_order_id=p.order_id')
            ->where($where)
            ->field('sum(ro.total-ro.diagnosis_cost) total')
            ->find();
        $redis = S(array('type' => 'redis'));
        S($this->sqlname, $sql);

        $orders=M('repair_order ro')->join('left join zsf_user_real_info ifo on ro.user_id=ifo.user_id')
            ->join('left join zsf_volume_list l on ro.volume_list_id=l.volume_list_id')
            ->join('left join zsf_rewards_money m on ro.repair_order_num_alias=m.order_id')
            ->join('left join zsf_repair_order_protocol p on ro.repair_order_num_alias=p.order_id')
            ->where($where)
            ->field('ro.pay_time,ro.fault_money,p.maintain,p.bond,ro.repair_order_num_alias,ro.repair_order_id,ifo.id_name,ro.worker_tel,ro.total-ro.diagnosis_cost total,l.total vtotal,ro.diagnosis_cost,ro.tip,m.money')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
      //  var_dump($total);exit;
        $show       = $Page->show();
        $this->assign('list',$orders);
        $this->assign('total',$total['total']);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function excelout(){
        $redis = S(array('type' => 'redis'));
        $sql=S($this->sqlname);
        //设置列名称
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'序号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'订单编号','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'师傅信息','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'故障金额','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'优惠券','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'诊断费','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'小费','size'=>'20'),
            'H'=>array('index'=>'H1','name'=>'订单总金额(元)','size'=>'20'),
            'I'=>array('index'=>'I1','name'=>'佣金(-)或奖励(+)','size'=>'20'),
            'J'=>array('index'=>'J1','name'=>'师傅总收入','size'=>'20'),
            'K'=>array('index'=>'K1','name'=>'维修金（元）','size'=>'20'),
            'L'=>array('index'=>'L1','name'=>'保证金（元）	','size'=>'20'),
            'M'=>array('index'=>'M1','name'=>'支付时间','size'=>'20'),
        );
        //设置标题
        $title='维修订单资金管理表';
        //选择类型
        $type=2;
        //执行sql
        $data = M()->query($sql);
        phpExcel($sheetindex,$data,$title,$type);
        exit;
    }
}